package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.InvoiceManagerBean;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.NetUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/22.
 * 发票管理
 */

public class InvoiceManagerActivity extends BaseActivity2 implements SwipeRefreshLayout.OnRefreshListener {


    public static final String INVOICE_AMOUNT = "INVOICE_AMOUNT";

    public static final String INVOICE_ID = "INVOICE_ID";
    private RecyclerView invoice_manager_irv;
    private BBswipeRefreshLayout mSwipeRefreshLayout;
    private InvoiceManagerRcvAdapter mInvoiceManagerRcvAdapter;
    private CheckBox rb_all_check;
    private TextView tv_invoice_mount;
    private TextView tv_invoice_mount_hint;
    private TextView tv_invoice_explain;
    private Button btn_bill;
    private LinearLayout ll_select;
    private List<InvoiceManagerBean> mListBeans;
    private boolean isChecked;
    private ArrayList<InvoiceManagerBean> checkedList = new ArrayList<>();

    //开票数量
    private int billCount = 0;

    //发票金额
    private float invoiceAmount;

    private EntityBean[] mInvoiceManagerbeans;

    private ArrayList<String> invoiceIds;

    private boolean selected;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_invoice_manager;
    }

    @Override
    public void initView() {
        invoice_manager_irv= (RecyclerView) findViewById(R.id.recyview_invoice_manager);
        mSwipeRefreshLayout = (BBswipeRefreshLayout) findViewById(R.id.swipe_layout_invoice_manager);
        rb_all_check = (CheckBox) findViewById(R.id.rb_all_check);
        tv_invoice_mount = (TextView) findViewById(R.id.tv_invoice_mount);
        tv_invoice_mount_hint = (TextView) findViewById(R.id.tv_invoice_mount_hint);
        tv_invoice_explain = (TextView) findViewById(R.id.tv_invoice_explain);
        ll_select = (LinearLayout) findViewById(R.id.ll_select);
        btn_bill = (Button) findViewById(R.id.btn_bill);
        initRec();
        initData();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }


    public void initRec() {
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mInvoiceManagerRcvAdapter = new InvoiceManagerRcvAdapter();
        invoice_manager_irv.setLayoutManager(new LinearLayoutManager(mContext));
        invoice_manager_irv.setAdapter(mInvoiceManagerRcvAdapter);
        //条目的点击事件
        mInvoiceManagerRcvAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                //点击条目勾选中checkbox
                selected = mListBeans.get(position).isSelected();
                if(selected){
                    mListBeans.get(position).setSelected(false);
                    billCount--;
                    invoiceAmount -= mListBeans.get(position).getPayMoney();

                    if(billCount <= 0){
                        btn_bill.setEnabled(false);
                        btn_bill.setText("开票");
                        tv_invoice_explain.setVisibility(View.GONE);
                    }else {
                        btn_bill.setText("开票"+"("+billCount+")");
                        if(invoiceAmount-500>0){
                            tv_invoice_explain.setVisibility(View.GONE);

                        }else{
                            tv_invoice_explain.setVisibility(View.VISIBLE);
                        }
                    }
                    rb_all_check.setChecked(false);
                    isChecked = rb_all_check.isChecked();
                    tv_invoice_mount.setText("¥"+invoiceAmount + "");
                    checkedList.remove(mListBeans.get(position));
                }else {
                    mListBeans.get(position).setSelected(true);
                    billCount++;
                    invoiceAmount += mListBeans.get(position).getPayMoney();
                    if(invoiceAmount-500>0){
                        tv_invoice_explain.setVisibility(View.GONE);
                    }else{
                        tv_invoice_explain.setVisibility(View.VISIBLE);
                    }
                    if(billCount == mListBeans.size()){
                        rb_all_check.setChecked(true);
                        isChecked = rb_all_check.isChecked();
                    }
                    tv_invoice_mount.setText("¥"+invoiceAmount + "");
                    btn_bill.setEnabled(true);
                    btn_bill.setText("开票"+"("+billCount+")");
                    checkedList.add(mListBeans.get(position));
                }
            }
        });

        mInvoiceManagerRcvAdapter.refresh(mListBeans,this);
    }

    @Override
    public void onRefresh() {
        clientRequest();
    }

    private void initData() {
        if (NetUtil.checkNetEnable()) {
            mSwipeRefreshLayout.setRefreshing(true);
            clientRequest();
        }
        isChecked = rb_all_check.isChecked();
        ll_select.setOnClickListener(this);
        btn_bill.setOnClickListener(this);
        btn_bill.setEnabled(false);
        btn_bill.setText("开票");
        setToolbarTitle(AppUtil.getString(R.string.invoice_manager));
        setToolbarRightTextTitle(AppUtil.getString(R.string.bill_history));
    }

    private void clientRequest() {
        EntityBean bean = new EntityBean();
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "getNoInvoiceChargelistByCardno", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    parse(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {
                mSwipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    @Override
    public void onToolbarRightTvClick() {
        super.onToolbarRightTvClick();
        startActivity(BillHistoryActivity.class);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            //处理全选按钮
            case R.id.ll_select:
                isChecked = !isChecked;
                if (isChecked) {
                    billCount = 0;
                    invoiceAmount = 0;
                    checkedList.addAll(mListBeans);
                    rb_all_check.setChecked(true);
                }else {
                    checkedList.clear();
                    rb_all_check.setChecked(false);
                }
                for (InvoiceManagerBean bean : mListBeans) {
                    if(isChecked){
                        billCount++;
                        invoiceAmount += bean.getPayMoney();
                        if(invoiceAmount-500>0){
                            tv_invoice_explain.setVisibility(View.GONE);

                        }else{
                            tv_invoice_explain.setVisibility(View.VISIBLE);
                        }
                        tv_invoice_mount.setText("¥"+invoiceAmount + "");
                        btn_bill.setEnabled(true);
                        btn_bill.setText("开票"+"("+billCount+")");
                    }else{
                        billCount--;
                        invoiceAmount -= bean.getPayMoney();
                        if(invoiceAmount-500>0){
                            tv_invoice_explain.setVisibility(View.GONE);

                        }else{
                            tv_invoice_explain.setVisibility(View.VISIBLE);
                        }
                        tv_invoice_mount.setText("¥"+invoiceAmount + "");
                        btn_bill.setEnabled(false);
                        btn_bill.setText("开票"+"("+billCount+")");
                    }

                    bean.setSelected(isChecked);

                }
                mInvoiceManagerRcvAdapter.refresh(mListBeans,this);
                break;

            //处理开票按钮
            case R.id.btn_bill:
                //跳转到收件信息填写界面
                openInvoiceInfo();
        }
    }


    public void openInvoiceInfo(){
        invoiceIds = new ArrayList<>();
        for (int i = 0; i < checkedList.size(); i++) {
            String id = checkedList.get(i).getId().toString();
            invoiceIds.add(id);
        }
        Intent intent = new Intent(this, InvoiceInfoEditActivity.class);
        intent.putExtra(INVOICE_AMOUNT,invoiceAmount+"");
        intent.putStringArrayListExtra(INVOICE_ID,invoiceIds);
        startActivity(intent);
    }


    private void parse(Object result) {
        rb_all_check.setChecked(false);
        billCount = 0;
        invoiceAmount = 0;
        tv_invoice_mount.setText("¥"+invoiceAmount + "");
        tv_invoice_explain.setVisibility(View.GONE);
        btn_bill.setEnabled(false);
        isChecked=false;
        btn_bill.setText("开票");
        mListBeans = new ArrayList<>();
        EntityBean bean = (EntityBean) result;
        mInvoiceManagerbeans = (EntityBean[]) bean.get("result");

        for (int i = 0; i <mInvoiceManagerbeans.length ; i++) {
            InvoiceManagerBean invoiceManagerBean = new InvoiceManagerBean();
            invoiceManagerBean.setInvoiceHeader(mInvoiceManagerbeans[i].get("planname").toString());
            invoiceManagerBean.setPayTime(mInvoiceManagerbeans[i].get("createtime").toString());
            invoiceManagerBean.setPayMoney(Float.parseFloat(mInvoiceManagerbeans[i].get("feereally").toString()));
            invoiceManagerBean.setId(mInvoiceManagerbeans[i].get("id").toString());
            invoiceManagerBean.setSelected(false);
            mListBeans.add(invoiceManagerBean);
        }

        mInvoiceManagerRcvAdapter.refresh( mListBeans,this);
    }

}
